/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sinaEx;

import com.mysql.jdbc.Statement;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.ArrayList;
import java.util.List;
import weibo4j.Users;
import weibo4j.Weibo;
import weibo4j.model.User;
import weibo4j.model.WeiboException;

/**
 *
 * @author Administrator
 */
public class completement {

    public static List<String> id = new ArrayList<String>();

    public static void main(String[] args) throws IOException, WeiboException {
        readfile();
        prepareWeibo();
        int i=0;
        for (String uid : id) {
            System.out.println(i++);
            String statusCount = getStatus(uid);
            String query = "update `weibonews`.`wb_ex_network` set `statusCount`='" + statusCount + "' where `uid`='" + uid + "';";
            try {
                Class.forName("com.mysql.jdbc.Driver").newInstance();
                Connection conn = (Connection) DriverManager.getConnection("jdbc:mysql://192.168.8.6:3306/weibonews?useUnicode=true&characterEncoding=gb2312", "root", "root");
                Statement stmt = (Statement) conn.createStatement();
                stmt.executeUpdate(query);
            } catch (Exception e) {
                e.printStackTrace();
                break;
            }
        }
    }

    public static String getStatus(String id) throws WeiboException {
        Users um = new Users();
        String count = "0";
        User u = null;
        try {
            u = um.showUserById(id);
        } catch (Exception e) {
            String msg = e.getMessage();
            System.out.println(msg);
            if (u == null) {
                System.out.println("timeout...");
                int i = 0;
                while (u == null) {
                    
                    u = um.showUserById(id);
                    if (u == null) {
                        System.out.println("timeout...");
                    }
                    i += 1;
                    if (i > 100) {
                        break;
                    }
                }
            }
        }
        if (u != null) {
            count = u.getStatusesCount() + "";
        }
        return count;
    }

    public static void prepareWeibo() {
        String accessToken = spider.Auth4Code.getCode();
        Weibo weibo = new Weibo();
        weibo.setToken(accessToken);
    }

    public static void readfile() throws IOException {
        String filename = "src/sinaEx/ex_id_list";
        BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(filename)));
        String line = reader.readLine();
        line = reader.readLine();

        while (line != null) {
            id.add(line);
            line = reader.readLine();
        }
        reader.close();
    }
}
